import random

# class base for controller
from google.appengine.ext import webapp

# needed for display and lange positions
from tools import exposition_manager, language_manager

from model import Exposition

class Index(webapp.RequestHandler):
    '''
    '''

    def get(self):
        '''
        '''
        # get the language id
        locale_id = self.request.get('language')
        # get the size name
        size = self.request.get('size')

        #
        co_manager = exposition_manager.ExpositionManager(
                locale_id=locale_id, size=size)

        #
        query = Exposition().all()
        query.filter('language =', co_manager.language)
        #
        number_of_expositions = query.count()

        #
        exposition_key = None

        if number_of_expositions > 0:
            # random offset to get the exposition
            offset = random.randint(0, number_of_expositions - 1)
            # get a random exposition
            exposition = query.fetch(1, offset=offset)[0]
            # get the exposition key
            exposition_key = exposition.key()

        # display the exposition to show
        template = co_manager.show(exposition_key=exposition_key)
        return self.response.out.write(template)

    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size key to use
        size_key = self.request.get('size_key')

        #
        co_manager = exposition_manager.ExpositionManager(
                language_key=language_key, size_key=size_key)

        # retrieve the exposition key for display
        exposition_key = self.request.get('exposition_key')

        # retrieve the title from posting
        title = self.request.get('title')
        if not title == '':
            # find the corresponding key
            exposition_key = co_manager.get_exposition_key(title)

        # display the exposition to show
        template = co_manager.show(exposition_key)
        return self.response.out.write(template)

class Edit(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size key to use
        size_key = self.request.get('size_key')

        #
        co_manager = exposition_manager.ExpositionManager(
                language_key=language_key, size_key=size_key)

        exposition_key = None

        if not self.request.get('new') == '':
            #
            exposition_key = co_manager.new()

        if not self.request.get('edit') == '':
            # get title
            exposition_key = self.request.get('exposition_key')

        # retrieve the title from posting
        title = self.request.get('title')
        if not title == '':
            # find the corresponding key
            exposition_key = co_manager.get_exposition_key(title)

        if exposition_key:
            # display the exposition to edit
            template = co_manager.edit(exposition_key)
            return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size key to use
        size_key = self.request.get('size_key')

        #
        co_manager = exposition_manager.ExpositionManager(
                language_key=language_key, size_key=size_key)

        # retrieve the exposition title of the category
        exposition_key = self.request.get('exposition_key')

        # delete
        if not self.request.get('delete') == '':
            # tools delete the exposition
            co_manager.delete(exposition_key)

            # go to the index page
            return self.redirect('/exposition?language=%s&size=%s' % (
                co_manager.language.locale_id,
                co_manager.size.width))

        elif not self.request.get('new') == '':
            #
            exposition_key = co_manager.new()

        # cancel
        elif not self.request.get('cancel') == '':
            # display the exposition to edit
            template = co_manager.show(exposition_key)
            return self.response.out.write(template)

        # we save and display the exposition
        else:
            # get title
            title = self.request.get('title')

            # get info
            info = self.request.get('info')

            # tools save the exposition
            co_manager.save(
                    exposition_key, title, info)

        # display the exposition to edit
        template = co_manager.edit(exposition_key)
        return self.response.out.write(template)

class Update_date(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # retrieve the current language
        language_key = self.request.get('language_key')
        # get the size key to use
        size_key = self.request.get('size_key')

        #
        co_manager = exposition_manager.ExpositionManager(
                language_key=language_key, size_key=size_key)

        # retrieve the exposition title of the category
        exposition_key = self.request.get('exposition_key')

        # flag for generic ethod for start and end
        calendar_type = self.request.get('calendar_type')
        type_start = calendar_type=='date_start'

        # retrieve the exposition date start or end it depends
        date = self.request.get(calendar_type)

        # check for year inc or decrease
        if not self.request.get('year_down') == '' \
        or not  self.request.get('year_up') == '':
            # inc or dec flag
            up = not  self.request.get('year_up')==''

            # date update
            co_manager.save_date_year(
                    exposition_key, type_start=type_start, up=up)

        # check for month inc or decrease
        elif not self.request.get('month_down') == '' \
        or not  self.request.get('month_up') == '':
            # inc or dec flag
            up = not  self.request.get('month_up')==''

            # date update
            co_manager.save_date_month(
                    exposition_key, type_start=type_start, up=up)

        # check for day change
        elif not self.request.get('day') == '':
            # get the new day
            day = self.request.get('day')

            # date update
            co_manager.save_date_day(
                    exposition_key, day, type_start=type_start)

        # update date
        else:
            co_manager.save_date_from_str(
                    exposition_key, date, type_start=type_start)

        # display the exposition currently edited
        template =  co_manager.edit(exposition_key)
        return self.response.out.write(template)
